#!/usr/bin/env node
//网课
//javascript的原型继承
// const Dog = require('./02-dog');

// let taidi = new Dog('taidi');
// let jingba = new Dog('jingba');
// let energy = 8;

// taidi.on('bark', onBark);
// jingba.on('bark', onBark);

// function onBark() {
//   console.log(this.name + 'barked!');

//   energy--;
//   if (energy <= 0) {
//     this.stop();
//     console.log(this.name + 'is tired!');
//   }
// }
import { Dog } from './02-dog.js';

// 实例化多个 Dog 对象
const dog1 = new Dog('Buddy', 3);
const dog2 = new Dog('Max', 5);
const dog3 = new Dog('Charlie', 2);

// 相同的 bark 事件处理函数
const barkHandler = (name, energy) => {
  console.log(`${name} barked! Energy left: ${energy}`);
};

// 注册 bark 事件处理函数
dog1.on('bark', barkHandler);
dog2.on('bark', barkHandler);
dog3.on('bark', barkHandler);

// 修改 Dog 的 energy
setTimeout(() => {
  dog1.energy = 0; // Buddy stops barking
  dog2.energy = 10; // Max has more energy to bark
}, 5000);
